package com.itheima.reggie.service.Impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.IdWorker;
import com.baomidou.mybatisplus.core.toolkit.StringUtils;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.itheima.reggie.builder.FullOrderData;
import com.itheima.reggie.builder.FullOrderDataBuilder;
import com.itheima.reggie.common.BaseConText;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.DishDto;
import com.itheima.reggie.dto.OrdersDto;
import com.itheima.reggie.entity.*;
import com.itheima.reggie.mapper.OrderMapper;
import com.itheima.reggie.service.*;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.math.BigDecimal;
import java.time.LocalDateTime;
import java.util.ArrayList;
import java.util.List;
import java.util.UUID;

@Service
@Slf4j
public class OrderServiceImpl extends ServiceImpl<OrderMapper, Orders> implements OrderService {

    @Autowired
    private ShoppingCartService shoppingCartService;
    @Autowired
    private UserService userService;
    @Autowired
    private AddressBookService addressBookService;
    @Autowired
    private OrderDetailService orderDetailService;
    @Transactional
    public void submit(Orders orders) {
        Long userId = BaseConText.getCurrentId();
        orders.setUserId(userId);
        long id = IdWorker.getId();
        orders.setId(id);
        FullOrderDataBuilder fullOrderDataBuilder =
                new FullOrderDataBuilder(userService,shoppingCartService,orders,addressBookService);
        FullOrderData fullOrderData =fullOrderDataBuilder.builderOrder()
                .builderOrderDetail()
                .builder();
        Orders ordersInfo = fullOrderData.getOrders();
        List<OrderDetail> orderDetailList = fullOrderData.getOrderDetailList();
        this.save(ordersInfo);
        orderDetailService.saveBatch(orderDetailList);
        shoppingCartService.remove(new LambdaQueryWrapper<ShoppingCart>().eq(ShoppingCart::getUserId,userId));
//        QueryWrapper<ShoppingCart> queryWrapper = new QueryWrapper<>();
//        queryWrapper.eq("user_id",userId);
//        //1.查询购物车
//        List<ShoppingCart> list = shoppingCartService.list(queryWrapper);
//        BigDecimal bigDecimal = new BigDecimal("0");
//        ArrayList<OrderDetail> orderDetails = new ArrayList<>();
//        for (ShoppingCart shoppingCart : list) {
//            bigDecimal=bigDecimal.add(shoppingCart.getAmount().multiply(new BigDecimal(shoppingCart.getNumber())));
//            OrderDetail orderDetail = new OrderDetail();
//            BeanUtils.copyProperties(shoppingCart,orderDetail,"id");
//            orderDetails.add(orderDetail);
//        }
//
//        AddressBook byId = addressBookService.getById(orders.getAddressBookId());
//        User user = userService.getById(userId);
//        orders.setNumber(UUID.randomUUID().toString());
//        orders.setStatus(2);
//        orders.setUserId(userId);
//        orders.setOrderTime(LocalDateTime.now());
//        orders.setCheckoutTime(LocalDateTime.now());
//        orders.setAmount(bigDecimal);
//        orders.setPhone(byId.getPhone());
//        orders.setAddress(byId.getDetail());
//        orders.setConsignee(byId.getConsignee());
//        orders.setUserName(user.getName());
//        //2.清空购物车
//        shoppingCartService.remove(queryWrapper);
//        this.save(orders);
//        orderDetails.forEach(orderDetail -> {
//            orderDetail.setOrderId(orders.getId());
//        });
//        orderDetailService.saveBatch(orderDetails);
    }
}